﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using agent02.com.somee.g02travel;
using agent02.Model;

namespace agent02.Controllers
{
    public class TravelController : Controller
    {
        AgentDBEntities db = new AgentDBEntities();
        //
        // GET: /Travel/
        public ActionResult Index()
        {
            WSTravel ws = new WSTravel();
            Location[] l = ws.WSTravel_Location();
            var country = (from x in l
                          select x.Country).Distinct();
            return View(country.ToList());
        }

        //
        // GET: /Travel/Country/CountryName
        public ActionResult Country(string id)
        {
            WSTravel ws = new WSTravel();
            Location[] locationList = ws.WSTravel_Location();
            var location = (from x in locationList
                          where x.Country == id
                          select x.LocationName).Distinct();
            return View(location.ToList());
        }

        //
        // GET: /Travel/Location/LocationName
        public ActionResult Location(string id)
        {
            WSTravel ws = new WSTravel();
            TravelSearchResultModel tsr = new TravelSearchResultModel();

            ListTour[] listTour1 = ws.WSTravel1("", id, "");
            ListTour[] listTour2 = ws.WSTravel1("", "", id);

            tsr.listTour = listTour1.Union(listTour2).ToArray();

            return View(tsr);
        }

        //
        // GET: /Travel/Search
        public ActionResult Search()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(TravelSearchModel trs)
        {
            return RedirectToAction("SearchResult", new { tourtype = trs.tourType, departure = trs.departure, destination = trs.destination });
        }

        //
        // GET: /Travel/SearchResult
        public ActionResult SearchResult(string tourType, string departure, string destination)
        {
            WSTravel ws = new WSTravel();
            TravelSearchResultModel tsr = new TravelSearchResultModel();
            tsr.listTour = ws.WSTravel1(tourType, departure, destination);
            return View(tsr);
        }

        //
        // GET: /Travel/AdvanceSearchResult
        public ActionResult AdvanceSearchResult(string tourType, DateTime dateFrom, DateTime dateTo, float priceFrom, float priceTo, string location)
        {
            WSTravel ws = new WSTravel();
            TravelSearchResultModel tsr = new TravelSearchResultModel();
            tsr.listTour = ws.WSTravel2(tourType, dateFrom, dateTo, priceFrom, priceTo, location);
            return View(tsr);
        }

        //
        // GET: /Travel/Details/TRIP000001
        public ActionResult Details(string tripID)
        {
            WSTravel ws = new WSTravel();
            TravelTourDetailModel t = new TravelTourDetailModel();
            t.tour = ws.WSTravel3(tripID);
            t.air = ws.WSTravel_Airplane(tripID);
            return View(t);
        }

        //
        // GET: /Travel/AdvanceSearch
        public ActionResult AdvanceSearch()
        {
            TravelAdvanceSearchModel t = new TravelAdvanceSearchModel();
            return View("AdvanceSearch", t);
        }

        [HttpPost]
        public ActionResult AdvanceSearch(TravelAdvanceSearchModel trs)
        {
            return RedirectToAction("AdvanceSearchResult", new { tourType = trs.tourType, dateFrom = trs.dateFrom, dateTo = trs.dateTo, priceFrom = trs.priceFrom, priceTo = trs.priceTo, location = trs.location });
        }

        //
        // GET: /Travel/Buy/TRIP000001
        public ActionResult Buy(string id, bool air)
        {
            TravelBuyModel b = new TravelBuyModel();
            return View("Buy", b);
        }

        [HttpPost]
        public ActionResult Buy(string id, bool air, TravelBuyModel b)
        {
            if (air)
            {
                return RedirectToAction("CustomerRegister", new { id = id, adults = b.adultsNum, children = b.childenNum });
            }
            else
            {
                return RedirectToAction("Invoice", new { id = id, adults = b.adultsNum, children = b.childenNum });
            }
        }

        public ActionResult CustomerRegister(string id, int adults, int children)
        {
            Customer[] customers = new Customer[adults + children];
            
            return View(customers);
        }

        public ActionResult Invoice(string id, int adults, int children)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Buy2()
        {
            WSTravel ws = new WSTravel();
            var acc = from p in db.WEB_SERVICE
                      where p.MaWS == "WS001"
                      select p;
            if (acc.Count() == 1)
            {
                string username = acc.Single().Username;
                string password = acc.Single().Password;
                if (ws.WSTravel0(username, password))
                {
                    //ws.WSTravel4(id, quantity, 
                }

            }

            return View();
        }
    }
}
